package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.salesmanage.util.OrganizeCustom;
import com.turing.post.salesmanage.util.OrganizeCustomDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 同一购卡人使用转账方式购卡，转账账户名称与购卡人名称不符的
 * 
 * @author 王浪
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule15")
public class AlarmRule15 implements AlarmRulesBusiness {

	@EJB(mappedName = "salesmanage.impl.OrganizeCustomDaoImpl")
	private OrganizeCustomDao organizeCustomDaoImpl;
	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;
	@EJB(mappedName = "	salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;
	@EJB(mappedName = "	capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;
	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;

	private Map<String, String> perMap = new HashMap<String, String>();// 个人map
	private Map<String, String> orgMap = new HashMap<String, String>();// 单位

	// 同一购卡人使用转账方式购卡，转账账户名称与购卡人名称不符的

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {

		// 1.查询销售单是转账方式的
		List<Object[]> list = saleOrderDaoImpl.getSaleOrderForAlarm(tranDate,
				issueCompanyNo);
		SaleOrder alarmOrder = null;// 销售单
		String personname = null;// 个人信息
		Map<String, BankAccount> bankAccMap = new HashMap<String, BankAccount>();// 账户map
		BankAccount bankAcc = null;// 账户
		Object[] obj = null;
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				obj = list.get(i);
				// 个人
				if (obj[1] != null) {
					personname = getPersonName(obj[1].toString());
				}
				// 单位
				else if (obj[2] != null) {
					personname = getOrganizeCustom(obj[2]
							.toString());
				}
				if (personname == null) {
					continue;
				}
				// 付款账户
				if (obj[3] == null) {
					continue;
				}
				if (bankAccMap.get(obj[3].toString()) == null) {
					bankAcc = bankAccountDaoImpl
							.getBankAccountByBankAccountId(new BigInteger(
									obj[3].toString()));
					if (bankAcc == null) {
						continue;
					}
					bankAccMap.put(obj[3].toString(), bankAcc);
				} else
					bankAcc = bankAccMap.get(obj[3].toString());
				// 2.找出购卡人和付款人信息不一致的(存在风险交易)
				if (!bankAcc.getAccountName().equals(personname)) {
					alarmOrder = saleOrderDaoImpl
							.getSaleOrderBySaleOrderId(new BigInteger(obj[0]
									.toString()));
					releaseAlarm(alarmOrder, issueCompanyNo, alarmRule,
							tranDate, personname);// 发布风险信息
				}
			}

		}

	}

	/**
	 * 发布风险信息
	 * 
	 * @param tranDate
	 * @param alarmRule
	 * @param issueCompany
	 * 
	 * @param alarmOrders存在风险的销售单
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(SaleOrder alarmOrder, String issueCompanyNo,
			AlarmRule alarmRule, Date tranDate, String personname)
			throws PCPSApplicationException {
		SaleBranch saleBranch = saleBranchDaoImpl
				.getSaleBranchBySaleBranchNo(alarmOrder.getSaleBranchNo());

		// 插入预警信息
		AlarmInfo alarmInfo = new AlarmInfo();
		alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
		alarmInfo.setIssueCompanyNo(issueCompanyNo);
		alarmInfo.setBranchNo(alarmOrder.getSaleBranchNo());
		alarmInfo.setBranchName(saleBranch.getSaleBranchName());
		alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
		alarmInfo.setRiskCustomName(personname);
		alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
		alarmInfo.setDealStatus("0");

		BigInteger alarmInfoId = alarmInfoDaoImpl.getMaxAlarmInfoId();
		alarmInfo.setAlarmInfoId(alarmInfoId);
		alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);

		// 插入预警明细
		AlarmInfoDetail alarmInfoDetail = new AlarmInfoDetail();
		alarmInfoDetail.setAlarmInfoId(alarmInfoId);
		alarmInfoDetail.setSeqNo(0);
		alarmInfoDetail.setBusinessType("0");
		alarmInfoDetail.setBusinessOrderId(alarmOrder.getSaleOrderId());
		alarmInfoDetail.setTranDate(tranDate);
		alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
	}

	private String getPersonName(String personid)
			throws PCPSApplicationException {
		if (personid == null || "".equals(personid)) {
			return null;
		}
		if (perMap.get(personid) == null) {
			PersonalCustom person = personalCustomDaoImpl
					.getPersonalCustomByPersonalId(new BigInteger(personid));
			if (person == null) {
				return null;
			}
			perMap.put(personid, person.getName());
			return person.getName();
		} else {
			return perMap.get(personid);
		}
	}

	private String getOrganizeCustom(String orgid)
			throws PCPSApplicationException {
		if (orgMap.get(orgid) == null) {
			OrganizeCustom organizeCustom = organizeCustomDaoImpl
					.getOrganizeCustomByOrganizeId(new BigInteger(orgid));
			if (organizeCustom == null) {
				return null;
			}
			orgMap.put(orgid, organizeCustom.getFullName());
			return organizeCustom.getFullName();
		} else {
			return orgMap.get(orgid);
		}
	}
}
